package com.codes.service.impl;

import com.codes.dao.UserMapper;
import com.codes.dao.UserRoleMapper;
import com.codes.model.User;
import com.codes.model.UserRole;
import com.codes.service.UserSV;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Date 2020/5/26 10:58
 * @Created by peanut
 */
@Service
public class UserImpl implements UserSV {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        //  同时删除 角色关联表  by 用户id

        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return 0;
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public Map selectByAccount(User user) {
        return userMapper.selectByAccount(user);
    }

    @Override
    public List<Map<String, Object>> getAllUser(Map<String, Object> map) {
        return userMapper.getAllUser(map);
    }

    @Override
    public Map insertByMap(Map<String, Object> map) {
        // 保存 用户角色关联
        // 保存角色关联表 获取用户id
         userMapper.insertByMap(map);
        Integer userId = MapUtils.getInteger(map,"id");
        Integer roleId =MapUtils.getInteger(map,"roleId");
        //
        if(null != userId && null != roleId){
            UserRole ur = new UserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            userRoleMapper.insertSelective(ur);
        }
        return map;
    }

    @Override
    public int updateByMap(Map<String, Object> map) {
        Integer loginUpdate = null;
        loginUpdate = MapUtils.getInteger(map,"UpdateRole");
        if(null != loginUpdate && 1 == loginUpdate){  // 表示 修改用户,, 所以  修改用户角色
            userRoleMapper.updateByUserId(map);
        }
        return userMapper.updateByMap(map);
    }

    @Override
    public Map getUserInfo(Map m) {
        return userMapper.getUserInfo(m);
    }


}